package jrdcom.com.onecodesample.DB;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import jrdcom.com.onecodesample.Model.CityModel;
import jrdcom.com.onecodesample.Model.CountryModel;
import jrdcom.com.onecodesample.Model.ProvinceModel;

/**
 * Created by longcheng on 17/1/17.
 */
public class CoolWearherDB {
    private final static  String DB_NAME = "cool_weather";
    private final static int Version = 1;
    private SQLiteDatabase db;
    private static CoolWearherDB coolWearherDB;
    private CoolWearherDB(Context context){
        CoolWeatherOpenHelper coolWeatherOpenHelper = new CoolWeatherOpenHelper(context, DB_NAME, null,Version);
        db = coolWeatherOpenHelper.getWritableDatabase();
    }

    public synchronized static  CoolWearherDB getInstance(Context context){
        if(null == coolWearherDB){
            coolWearherDB = new CoolWearherDB(context);

        }
        return coolWearherDB;
    }

    //save province, city, country
    public void saveProvince(ProvinceModel provinceModel){
        ContentValues contentValues = new ContentValues();
        contentValues.put("province_name",provinceModel.getProvinceName());
        contentValues.put("province_code", provinceModel.getProvinceCode());
        db.insert("Province",null, contentValues);
    }

    public void saveCity(CityModel cityModel){
        ContentValues contentValues = new ContentValues();
        contentValues.put("city_name",cityModel.getCityName());
        contentValues.put("city_code",cityModel.getCityCode());
        contentValues.put("province_id",cityModel.getProvinceId());
        db.insert("City",null, contentValues);
    }

    public void saveCountry(CountryModel countryModel){
        ContentValues contentValues = new ContentValues();
        contentValues.put("country_name",countryModel.getCountryName());
        contentValues.put("country_code",countryModel.getCountryCode());
        contentValues.put("city_id",countryModel.getCityId());
        db.insert("Country",null, contentValues);

    }
    //get province, city, country
    public List<ProvinceModel> getProvinceList(){
        List<ProvinceModel> provinceModelList = new ArrayList<ProvinceModel>();
        Cursor cursor = db.query("Province",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            while (cursor.moveToNext()){
                ProvinceModel provinceModel = new ProvinceModel();
                provinceModel.setProvinceId(cursor.getInt(cursor.getColumnIndex("province_id")));
                provinceModel.setProvinceName(cursor.getString(cursor.getColumnIndex("province_name")));
                provinceModel.setProvinceCode(cursor.getString(cursor.getColumnIndex("province_code")));
                provinceModelList.add(provinceModel);
            }
        }
        return provinceModelList;
    }

    public List<CityModel> getCityList(int province_id){
        List<CityModel> cityModelList = new ArrayList<CityModel>();
        Cursor cursor = db.query("City",null,"province_id=?", new String[]{String.valueOf(province_id)},null,null,null);
        if(cursor.moveToFirst()){
            while (cursor.moveToNext()){
                CityModel cityModel = new CityModel();
                cityModel.setCityId(cursor.getInt(cursor.getColumnIndex("city_id")));
                cityModel.setCityName(cursor.getString(cursor.getColumnIndex("city_name")));
                cityModel.setCityCode(cursor.getString(cursor.getColumnIndex("city_code")));
                cityModel.setProvinceId(cursor.getInt(cursor.getColumnIndex("province_id")));
                cityModelList.add(cityModel);
            }
        }
        return cityModelList;
    }

    public List<CountryModel> getCountryList(int city_id){
        List<CountryModel> countryModelList  = new ArrayList<CountryModel>();
        Cursor cursor = db.query("Country",null,"city_id=?",new String[]{String.valueOf(city_id)},null,null,null);
        if(cursor.moveToFirst()){
            while (cursor.moveToNext()){
                CountryModel countryModel = new CountryModel();
                countryModel.setCountryId(cursor.getInt(cursor.getColumnIndex("country_id")));
                countryModel.setCountryName(cursor.getString(cursor.getColumnIndex("country_name")));
                countryModel.setCountryCode(cursor.getString(cursor.getColumnIndex("country_code")));
                countryModel.setCityId(cursor.getInt(cursor.getColumnIndex("city_id")));
                countryModelList.add(countryModel);
            }
        }
        return countryModelList;
    }
}
